package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

	@Autowired
	private IDestinationService destinationService;

	@RequestMapping("/list")
	public String query(Model model, @ModelAttribute("qo") DestinationQuery qo){
		model.addAttribute("page",destinationService.query(qo));
		// 调用查询当前位置以及它的父级
		List<Destination> destinations = destinationService.queryParent(new ArrayList<>(), qo.getParentId());
		// 该操作是把集合倒序,因为查出来的顺序刚好是反过来的
		Collections.reverse(destinations);
		model.addAttribute("toasts", destinations);
		return "/destination/list";
	}

	@RequestMapping("/listDests")
	@ResponseBody
	public Object listDests(){
		return destinationService.list();
	}

	@RequestMapping("/updateInfo")
	@ResponseBody
	public Object updateInfo(String id,String info){
		destinationService.updateInfo(id,info);
		return JsonResult.success();
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(String id){
		destinationService.delete(id);
		return JsonResult.success();
	}
}
